/*
 * Filename:    NoticeDocument.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年12月6日
 */
package com.paic.mhis.hcpms.warning.web.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.paic.mhis.hcpms.itextpdf.web.pdfmodel.DocumentModel;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsFontUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsParagraphUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfCellUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsPdfTableUtil;
import com.paic.mhis.hcpms.itextpdf.web.util.HcpmsRectangleUtil;

public class NoticeDocument implements DocumentModel {
	public static final String UNIT_NAME = "单位名称:";
	public static final String NOTIC_NUM = "通知单号:";
	public static final String WARN_CONTENT = "预\n警\n内\n容";
	public static final String WARN_PRIOD = "预警时段:";
	public static final String WARN_REQ = "预\n警\n要\n求";
	public static final String WARN_FROM_UNIT = "社保经办科室(盖章):";
	public static final String WARN_PARENT_DATE = "  年       月       日";
	public static final String WARN_TO_UNIT = "定点单位(盖章):";
	public static final String AUDIT_UNIT = "经办科室电话:";
	private static final String TITLE_NAME = "医保定点医疗机构、定点零售药店预警通知单";

	@Override
	public void createDocument(Document document) {
		document.setPageSize(HcpmsRectangleUtil.createNoticeRect());
	}

	@Override
	public void createContent(Document document, Map<String, Object> map) {

		Font font = HcpmsFontUtil.createTitleFont(12, Font.BOLD);
		Font fontText = HcpmsFontUtil.createTextFont(10, Font.NORMAL);
		try {
			float[] widths = { 0.1f, 0.9f };
			PdfPTable table = HcpmsPdfTableUtil.createTable(widths, 25, 95, Element.ALIGN_CENTER);
			table.addCell(HcpmsPdfCellUtil.createCellCol(
					HcpmsParagraphUtil.createParagraphText(font, getValue(map, "regionName") + TITLE_NAME), new int[] {
							5, 5, 5, 5 }, Element.ALIGN_CENTER, 2, Rectangle.NO_BORDER, 30f));

			float[] widthRow1 = { 0.6f, 0.4f };
			PdfPTable tableRow1 = new PdfPTable(widthRow1);
			tableRow1.setWidthPercentage(100);
			tableRow1.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			tableRow1.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, UNIT_NAME + getValue(map, "name")), new int[] { 4,
							4, 4, 2 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			tableRow1.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, NOTIC_NUM + getValue(map, "noticeNo")), new int[] {
							4, 4, 4, 2 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			PdfPCell cell1 = new PdfPCell();
			cell1.setColspan(2);
			cell1.addElement(tableRow1);
			table.addCell(cell1);
			table.addCell(HcpmsPdfCellUtil.createCellRow(
					HcpmsParagraphUtil.createParagraphTextLead(fontText, WARN_CONTENT), Element.ALIGN_CENTER, 2));

			table.addCell(HcpmsPdfCellUtil.createCellCenter(
					HcpmsParagraphUtil.createParagraphText(fontText, getValue(map, "content")),
					new int[] { 2, 2, 2, 2 }, Element.ALIGN_LEFT, 60f));

			table.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_PRIOD + getDateToString(map.get("datestart"))
							+ "至" + getDateToString(map.get("dateend"))), new int[] { 4, 4, 4, 2 }, Element.ALIGN_LEFT));

			table.addCell(HcpmsPdfCellUtil.createCellMiddle(HcpmsParagraphUtil.createParagraphText(fontText, WARN_REQ),
					Element.ALIGN_CENTER));
			table.addCell(HcpmsPdfCellUtil.createCellCenter(
					HcpmsParagraphUtil.createParagraphText(fontText, getValue(map, "requirement")), new int[] { 2, 2,
							2, 2 }, Element.ALIGN_LEFT, 60f));

			PdfPTable tableRow2 = new PdfPTable(widthRow1);
			tableRow2.setWidthPercentage(100);
			tableRow2.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			tableRow2.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_FROM_UNIT), new int[] { 4, 4, 4, 2 },
					Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			tableRow2.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_TO_UNIT), new int[] { 4, 4, 4, 1 },
					Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			PdfPCell cell2 = new PdfPCell();
			cell2.setColspan(2);
			cell2.addElement(tableRow2);
			table.addCell(cell2);

			PdfPTable tableRow3 = new PdfPTable(widthRow1);
			tableRow3.setWidthPercentage(100);
			tableRow3.setHorizontalAlignment(Element.ALIGN_MIDDLE);
			tableRow3.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, getDateToString(map.get("noticeDate"))),
					new int[] { 4, 4, 4, 60 }, Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			tableRow3.addCell(HcpmsPdfCellUtil.createCell(
					HcpmsParagraphUtil.createParagraphText(fontText, WARN_PARENT_DATE), new int[] { 4, 4, 4, 60 },
					Element.ALIGN_LEFT, Rectangle.NO_BORDER));
			PdfPCell cell3 = new PdfPCell();
			cell3.setColspan(2);
			cell3.addElement(tableRow3);
			table.addCell(cell3);

			table.addCell(HcpmsPdfCellUtil.createCellCol(HcpmsParagraphUtil.createParagraphText(fontText, AUDIT_UNIT),
					new int[] { 4, 4, 4, 100 }, Element.ALIGN_CENTER, 2, Rectangle.NO_BORDER));

			document.add(table);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void setPdfWriter(PdfWriter pdf, Map<String, Object> map) {
		// PdfContentByte cb = pdf.getDirectContent();
		// cb.beginText();
		// // 设置标题
		// Font font = HcpmsFontUtil.createTitleFont(14, Font.BOLD);
		// Phrase phrase = HcpmsPhraseUtil.createPhraseTitle(font, getValue(map,
		// "title"));
		// HcpmsColumnUtil. (cb, phrase, 20, 550, 820, 530, 15,
		// Element.ALIGN_CENTER);
		// // 设置内容
		// Font fontText = HcpmsFontUtil.createTextFont(12, Font.NORMAL);
		//
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, UNIT_NAME),
		// HcpmsPhraseUtil.createPhraseText(fontText, getValue(map, "name")),
		// 100, 520, 400, 500,
		// Element.ALIGN_LEFT);
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, NOTIC_NUM),
		// HcpmsPhraseUtil.createPhraseText(fontText, "预警医(药)字" + getValue(map,
		// "warnNo") + "号"), 500, 520, 780,
		// 500, Element.ALIGN_LEFT);
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, WARN_CONTENT),
		// HcpmsPhraseUtil.createPhraseText(fontText, getValue(map, "content")),
		// 100, 490, 780, 450,
		// Element.ALIGN_LEFT);
		// HcpmsColumnUtil
		// .createColumnTextTitll(
		// cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, WARN_PRIOD),
		// HcpmsPhraseUtil.createPhraseText(fontText,
		// getValue(map, "datestart") + "至" + getValue(map, "dateend")), 100,
		// 440, 780, 420,
		// Element.ALIGN_LEFT);
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, WARN_REQ),
		// HcpmsPhraseUtil.createPhraseText(fontText, getValue(map,
		// "requirement")), 100, 410, 780, 370,
		// Element.ALIGN_LEFT);
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, WARN_FROM_UNIT),
		// HcpmsPhraseUtil.createPhraseText(fontText, "厦门市社会保险管理中心"), 120, 330,
		// 400, 310, Element.ALIGN_LEFT);
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, WARN_TO_UNIT),
		// HcpmsPhraseUtil.createPhraseText(fontText, ""), 470, 330, 780, 310,
		// Element.ALIGN_LEFT);
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, WARN_FROM_DATE),
		// HcpmsPhraseUtil.createPhraseText(fontText, getValue(map,
		// "noticeDate")), 120, 290, 400, 270,
		// Element.ALIGN_LEFT);
		// HcpmsColumnUtil.createColumnTextTitll(cb,
		// HcpmsPhraseUtil.createPhraseText(fontText, WARN_TO_DATE),
		// HcpmsPhraseUtil.createPhraseText(fontText, ""), 470, 290, 780, 270,
		// Element.ALIGN_LEFT);
		// cb.endText();
	}

	public static String getValue(Map<String, Object> map, String name) {
		String str = "";
		if (name == null || "".equals(name))
			return str;
		Object obj = map.get(name);
		if (obj == null || "".equals(obj))
			return str;
		if (obj instanceof Date) {
			SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd");
			str = sft.format(obj);
		} else if (obj instanceof List) {

		} else if (obj instanceof Map) {

		} else {
			str = obj.toString();
		}
		return str;
	}

	public static String getDateToString(Object obj) {
		if (obj == null || "".equals(obj)) {
			return WARN_PARENT_DATE;
		}
		if (obj instanceof Date) {
			SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月dd日");
			return sft.format(obj);
		} else if (obj instanceof String) {
			Date date = new Date();
			try {
				date = new SimpleDateFormat("yyyy-MM-dd").parse(obj.toString());
			} catch (ParseException e) {
				e.printStackTrace();
			}
			SimpleDateFormat sft = new SimpleDateFormat("yyyy年MM月dd日");
			return sft.format(date);
		}
		return "";
	}
}
